package com.hc.appanimal;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import com.hc.DTO.AccountDTO;
import com.hc.common.ActionEvent;
import com.hc.constants.Constants;
import com.hc.controller.AccountController;
import com.hc.model.utils.AccountModel;
import com.hc.ultil.PasswordValidator;
import com.hc.webservice.InternetUtils;
import com.hc.webservice.ServiceHandler;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	EditText edEmail, edFirstName, edLastName, edMobile, edPassword,
			edConfirmPassword;
	Button btnSigup, btnCheckEmail, btnCheckFirstName, btnCheckLastName,
			btnCheckMobile, btnCheckPassword, btnCheckConfirmPassword;
	TextView txtCheckPassword, txtCheckFirstName, txtCheckLastName,
			txtCheckMobile;
	Dialog dialog;
	AnimationDrawable frameAnimation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// WebServiceTask wst = new WebServiceTask(WebServiceTask.GET_TASK,
		// this, "GETting data...");

		// wst.execute(new String[] { sampleURL });
		// new VerySlowTaskUploadImages().execute();
		// Intent intent = new Intent(MainActivity.this, LoginActivity.class);
		// startActivity(intent);
		// AccountModel model = AccountModel.getInstance();
		// boolean flag = model.checkAccount(MainActivity.this);
		// if (flag) {
		// Intent intent = new Intent(MainActivity.this, LoginActivity.class);
		// startActivity(intent);
		// finish();
		// }
		edEmail = (EditText) findViewById(R.id.edEmail);
		edFirstName = (EditText) findViewById(R.id.edFirstName);
		edLastName = (EditText) findViewById(R.id.edLastName);
		edMobile = (EditText) findViewById(R.id.edMobile);
		edPassword = (EditText) findViewById(R.id.edPassword);
		edConfirmPassword = (EditText) findViewById(R.id.edConfirmPassword);
		btnSigup = (Button) findViewById(R.id.btnSigup);
		btnCheckEmail = (Button) findViewById(R.id.btnCheckEmail);
		btnCheckFirstName = (Button) findViewById(R.id.btnCheckFirstName);
		btnCheckLastName = (Button) findViewById(R.id.btnCheckLastName);
		btnCheckMobile = (Button) findViewById(R.id.btnCheckMobile);
		btnCheckPassword = (Button) findViewById(R.id.btnCheckPassword);
		btnCheckConfirmPassword = (Button) findViewById(R.id.btnCheckConfirmPassword);
		txtCheckPassword = (TextView) findViewById(R.id.txtCheckPassword);
		txtCheckFirstName = (TextView) findViewById(R.id.txtCheckFirstName);
		txtCheckLastName = (TextView) findViewById(R.id.txtCheckLastName);
		txtCheckMobile = (TextView) findViewById(R.id.txtCheckMobile);
		btnSigup.setOnClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	// register
	public class VerySlowTaskRegister extends AsyncTask<Void, Void, Void> {
		final ServiceHandler sh = new ServiceHandler();
		final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		AccountDTO dto;
		String errRegister = "";
		String jsonStr;

		public VerySlowTaskRegister(AccountDTO _dto) {
			dto = _dto;
			dialog = new Dialog(MainActivity.this, R.style.NewDialog);
			dialog.setContentView(R.layout.loading);

			dialog.setCancelable(false);
			dialog.findViewById(R.id.loading_icon).setBackgroundResource(
					R.drawable.spin_animation);
			frameAnimation = (AnimationDrawable) dialog.findViewById(
					R.id.loading_icon).getBackground();
			dialog.show();
			frameAnimation.start();
		}

		@Override
		protected Void doInBackground(Void... params) {

			nameValuePairs.add(new BasicNameValuePair("Email", dto.getEmail()));
			nameValuePairs.add(new BasicNameValuePair("FirstName", dto
					.getFirstName()));
			nameValuePairs.add(new BasicNameValuePair("LastName", dto
					.getLastName()));
			nameValuePairs.add(new BasicNameValuePair("MobileNo", dto
					.getMobileNo()));
			nameValuePairs.add(new BasicNameValuePair("Password", dto
					.getPassword()));
			nameValuePairs.add(new BasicNameValuePair("ConfirmPassword", dto
					.getConfirmPassword()));

			jsonStr = sh.makeServiceCall(Constants.URL_REGISTER,
					ServiceHandler.POST, nameValuePairs);
			if (!jsonStr.equalsIgnoreCase("")) {
				try {
					JSONObject object1;
					object1 = new JSONObject(jsonStr);
					JSONObject ModelState = object1.getJSONObject("ModelState");
					JSONArray arrModelState = ModelState.getJSONArray("");
					errRegister = arrModelState.getString(1);

				} catch (Exception e) {

				}
			} else {

			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			frameAnimation.stop();
			if (jsonStr.equalsIgnoreCase("")) {
				ActionEvent e = new ActionEvent(MainActivity.this,
						Constants.INSERT_ACCOUNT, dto, null);
				AccountController.getInstance().handleViewEvent(e);
				Intent intent = new Intent(MainActivity.this,
						RegisterFinalActivity.class);
				startActivity(intent);
				finish();
			} else {
				final Dialog dialogerr = new Dialog(MainActivity.this,
						android.R.style.Theme_Translucent);
				dialogerr.requestWindowFeature(Window.FEATURE_NO_TITLE);

				dialogerr.setCancelable(true);
				dialogerr.setContentView(R.layout.dialog_err);
				TextView txtContent = (TextView) dialogerr
						.findViewById(R.id.txtContent);
				txtContent.setText(errRegister);
				Button btnOke = (Button) dialogerr.findViewById(R.id.bntOk);

				btnOke.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						dialogerr.dismiss();
					}
				});

				dialogerr.show();
			}

			// txtCheckEmail.setVisibility(View.VISIBLE);
			// txtCheckEmail.setText(errRegister);
		}

	}

	public static String excutePost(String targetURL, String postParameters) {
		// disableConnectionReuseIfNecessary();

		HttpURLConnection urlConnection = null;
		try {
			// create connection
			URL urlToRequest = new URL(targetURL);
			urlConnection = (HttpURLConnection) urlToRequest.openConnection();
			// urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
			// urlConnection.setReadTimeout(DATARETRIEVAL_TIMEOUT);

			// handle POST parameters
			if (postParameters != null) {

				urlConnection.setDoOutput(true);
				urlConnection.setRequestMethod("POST");
				urlConnection.setFixedLengthStreamingMode(postParameters
						.getBytes().length);
				urlConnection.setRequestProperty("Content-Type",
						"application/x-www-form-urlencoded");

				// send the POST out
				PrintWriter out = new PrintWriter(
						urlConnection.getOutputStream());
				out.print(postParameters);
				out.close();
			}

			// handle issues
			int statusCode = urlConnection.getResponseCode();
			if (statusCode != HttpURLConnection.HTTP_OK) {
				// throw some exception
			}

			// read output (only for GET)
			if (postParameters != null) {
				return null;
			} else {
				InputStream in = new BufferedInputStream(
						urlConnection.getInputStream());
				return getStringFromInputStream(in);
			}

		} catch (MalformedURLException e) {
			// handle invalid URL
		} catch (SocketTimeoutException e) {
			// hadle timeout
		} catch (IOException e) {
			// handle I/0
		} finally {
			if (urlConnection != null) {
				urlConnection.disconnect();
			}
		}

		return null;
	}

	public static String getStringFromInputStream(InputStream stream)
			throws IOException {
		int n = 0;
		char[] buffer = new char[1024 * 4];
		InputStreamReader reader = new InputStreamReader(stream, "UTF8");
		StringWriter writer = new StringWriter();
		while (-1 != (n = reader.read(buffer)))
			writer.write(buffer, 0, n);
		return writer.toString();
	}

	public final static boolean isValidEmail(CharSequence target) {
		return !TextUtils.isEmpty(target)
				&& android.util.Patterns.EMAIL_ADDRESS.matcher(target)
						.matches();
	}

	public boolean isValidInput(String email) {
		if (!isValidEmail(email)) {
			new AlertDialog.Builder(this)
					.setTitle("Error!")
					.setMessage("This email invalid, please input again.")
					.setPositiveButton(android.R.string.yes,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									// continue with delete
								}
							})
					/*
					 * .setNegativeButton(android.R.string.no, new
					 * DialogInterface.OnClickListener() { public void
					 * onClick(DialogInterface dialog, int which) { // do
					 * nothing } })
					 */
					.setIcon(android.R.drawable.ic_dialog_alert).show();
			return false;
		}
		// get 3 number first.

		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnSigup:
			if (InternetUtils.getInstance().isConnectingToInternet(this)) {
				Boolean flagEmail = false;
				Boolean flagFirstName = false;
				Boolean flagLastName = false;
				Boolean flagmobile = false;
				Boolean flagPassword = false;
				Boolean flagConfirmPassword = false;

				String strEmail = edEmail.getText().toString();
				String strFirstName = edFirstName.getText().toString();
				String strLastName = edLastName.getText().toString();
				String strMobile = edMobile.getText().toString();
				String strPassword = edPassword.getText().toString();
				String strConfirmPassword = edConfirmPassword.getText()
						.toString();

				if (strEmail.equalsIgnoreCase("") || !isValidInput(strEmail)) {
					btnCheckEmail.setVisibility(View.VISIBLE);
					btnCheckEmail.setBackgroundResource(R.drawable.check_false);
					flagEmail = false;
				} else {
					btnCheckEmail.setVisibility(View.VISIBLE);
					btnCheckEmail.setBackgroundResource(R.drawable.check_true);
					flagEmail = true;
				}
				if (strFirstName.equalsIgnoreCase("")) {

					btnCheckFirstName.setVisibility(View.VISIBLE);
					btnCheckFirstName
							.setBackgroundResource(R.drawable.check_false);
					flagFirstName = false;
				} else {
					if (strFirstName.length() < 2) {
						btnCheckFirstName.setVisibility(View.VISIBLE);
						btnCheckFirstName
								.setBackgroundResource(R.drawable.check_false);
						txtCheckFirstName.setVisibility(View.VISIBLE);
						txtCheckFirstName.setText("First Name is required");
						flagFirstName = false;
					} else {
						txtCheckFirstName.setVisibility(View.GONE);
						btnCheckFirstName.setVisibility(View.VISIBLE);
						btnCheckFirstName
								.setBackgroundResource(R.drawable.check_true);
						flagFirstName = true;
					}

				}
				if (strLastName.equalsIgnoreCase("")) {
					btnCheckLastName.setVisibility(View.VISIBLE);
					btnCheckLastName
							.setBackgroundResource(R.drawable.check_false);
					flagLastName = false;
				} else {
					if (strLastName.length() < 2) {
						btnCheckLastName.setVisibility(View.VISIBLE);
						btnCheckLastName
								.setBackgroundResource(R.drawable.check_false);
						txtCheckLastName.setVisibility(View.VISIBLE);
						txtCheckLastName.setText("Last Name is required");
						flagLastName = false;
					} else {
						txtCheckLastName.setVisibility(View.GONE);
						btnCheckLastName.setVisibility(View.VISIBLE);
						btnCheckLastName
								.setBackgroundResource(R.drawable.check_true);
						flagLastName = true;
					}

				}
				if (strMobile.equalsIgnoreCase("")) {
					btnCheckMobile.setVisibility(View.VISIBLE);
					btnCheckMobile
							.setBackgroundResource(R.drawable.check_false);
					flagmobile = false;
				} else {
					if (strMobile.length() < 10) {
						btnCheckMobile.setVisibility(View.VISIBLE);
						btnCheckMobile
								.setBackgroundResource(R.drawable.check_false);
						txtCheckMobile.setVisibility(View.VISIBLE);
						txtCheckMobile.setText("Mobile number is required");
						flagmobile = false;
					} else {
						txtCheckMobile.setVisibility(View.GONE);
						btnCheckMobile.setVisibility(View.VISIBLE);
						btnCheckMobile
								.setBackgroundResource(R.drawable.check_true);
						flagmobile = true;
					}

				}
				if (strPassword.equalsIgnoreCase("")) {
					btnCheckPassword.setVisibility(View.VISIBLE);
					btnCheckPassword
							.setBackgroundResource(R.drawable.check_false);
					flagPassword = false;
				} else {
					if (strPassword.length() < 6) {
						btnCheckPassword.setVisibility(View.VISIBLE);
						btnCheckPassword
								.setBackgroundResource(R.drawable.check_false);
						txtCheckPassword.setVisibility(View.VISIBLE);
						txtCheckPassword
								.setText("The Password must be at least 6 characters long");
						flagPassword = false;
					} else {
						txtCheckPassword.setVisibility(View.GONE);
						PasswordValidator checkpass = new PasswordValidator(
								MainActivity.this);
						if (checkpass.validate(strPassword) == false) {
							final Dialog dialogerr = new Dialog(
									MainActivity.this,
									android.R.style.Theme_Translucent);
							dialogerr
									.requestWindowFeature(Window.FEATURE_NO_TITLE);

							dialogerr.setCancelable(true);
							dialogerr.setContentView(R.layout.dialog_err);
							TextView txtContent = (TextView) dialogerr
									.findViewById(R.id.txtContent);
							txtContent
									.setText("Passwords must have at least one non letter or digit character. Passwords must have at least one lowercase ('a'-'z'). Passwords must have at least one uppercase ('A'-'Z').");
							Button btnOke = (Button) dialogerr
									.findViewById(R.id.bntOk);

							btnOke.setOnClickListener(new OnClickListener() {

								@Override
								public void onClick(View v) {

									dialogerr.dismiss();
								}
							});

							dialogerr.show();
							flagPassword = false;
						} else {
							btnCheckPassword.setVisibility(View.VISIBLE);
							btnCheckPassword
									.setBackgroundResource(R.drawable.check_true);
							flagPassword = true;
						}

					}

				}
				if (strConfirmPassword.equalsIgnoreCase("")
						|| !strConfirmPassword.equalsIgnoreCase(strPassword)) {
					btnCheckConfirmPassword.setVisibility(View.VISIBLE);
					btnCheckConfirmPassword
							.setBackgroundResource(R.drawable.check_false);
					flagConfirmPassword = false;
				} else {
					if (!flagPassword) {
						btnCheckConfirmPassword.setVisibility(View.VISIBLE);
						btnCheckConfirmPassword
								.setBackgroundResource(R.drawable.check_false);
						flagConfirmPassword = false;
					} else {
						btnCheckConfirmPassword.setVisibility(View.VISIBLE);
						btnCheckConfirmPassword
								.setBackgroundResource(R.drawable.check_true);
						flagConfirmPassword = true;
					}

				}
				AccountDTO dto = new AccountDTO();
				dto.setEmail(strEmail);
				dto.setFirstName(strFirstName);
				dto.setLastName(strLastName);
				dto.setMobileNo(strMobile);
				dto.setPassword(strPassword);
				dto.setConfirmPassword(strConfirmPassword);

				if (flagEmail && flagFirstName && flagLastName && flagmobile
						&& flagPassword && flagConfirmPassword) {
					if (InternetUtils.getInstance()
							.isConnectingToInternet(this)) {
						new VerySlowTaskRegister(dto).execute();
					}
				}
			} else {
				Toast.makeText(MainActivity.this, "no network",
						Toast.LENGTH_LONG).show();
			}

			break;

		default:
			break;
		}

	}

	private class WebServiceTask extends AsyncTask<String, Integer, String> {

		public static final int POST_TASK = 1;
		public static final int GET_TASK = 2;

		private static final String TAG = "WebServiceTask";

		// connection timeout, in milliseconds (waiting to connect)
		private static final int CONN_TIMEOUT = 3000;

		// socket timeout, in milliseconds (waiting for data)
		private static final int SOCKET_TIMEOUT = 5000;

		private int taskType = GET_TASK;
		private Context mContext = null;
		private String processMessage = "Processing...";

		private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();

		private ProgressDialog pDlg = null;

		public WebServiceTask(int taskType, Context mContext,
				String processMessage) {

			this.taskType = taskType;
			this.mContext = mContext;
			this.processMessage = processMessage;
		}

		public void addNameValuePair(String name, String value) {

			params.add(new BasicNameValuePair(name, value));
		}

		private void showProgressDialog() {

			pDlg = new ProgressDialog(mContext);
			pDlg.setMessage(processMessage);
			pDlg.setProgressDrawable(mContext.getWallpaper());
			pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			pDlg.setCancelable(false);
			pDlg.show();

		}

		@Override
		protected void onPreExecute() {

			// hideKeyboard();
			showProgressDialog();

		}

		protected String doInBackground(String... urls) {

			String url = urls[0];
			String result = "";

			HttpResponse response = doResponse(url);

			if (response == null) {
				return result;
			} else {

				try {

					result = inputStreamToString(response.getEntity()
							.getContent());

				} catch (IllegalStateException e) {
					Log.e(TAG, e.getLocalizedMessage(), e);

				} catch (IOException e) {
					Log.e(TAG, e.getLocalizedMessage(), e);
				}

			}

			return result;
		}

		@Override
		protected void onPostExecute(String response) {

			// handleResponse(response);
			pDlg.dismiss();

		}

		// Establish connection and socket (data retrieval) timeouts
		private HttpParams getHttpParams() {

			HttpParams htpp = new BasicHttpParams();

			HttpConnectionParams.setConnectionTimeout(htpp, CONN_TIMEOUT);
			HttpConnectionParams.setSoTimeout(htpp, SOCKET_TIMEOUT);

			return htpp;
		}

		private HttpResponse doResponse(String url) {

			// Use our connection and data timeouts as parameters for our
			// DefaultHttpClient
			HttpClient httpclient = new DefaultHttpClient(getHttpParams());

			HttpResponse response = null;

			try {

				switch (taskType) {

				case POST_TASK:
					HttpPost httppost = new HttpPost(url);
					// Add parameters
					httppost.setEntity(new UrlEncodedFormEntity(params));

					response = httpclient.execute(httppost);
					break;
				case GET_TASK:

					HttpGet httpget = new HttpGet(url);
					httpget.setHeader(
							"Authorization",
							"Bearer pOuJzg0Y0FtJpO-Ugson2P_8dbmvaDiBZs7Z-v_2eIgGgUfFsiWS-TOIkNlquCQGHuu3OyeWCFVIB7xJjEp6nKg9iY1-gzRfjHtdvBk7MVemtm6Ksf5AmmKmuZleLR_-bNMorwUOqTs0BCjAomM6h2peaXA3LTJ_-V6CDpnKVmmfTf0DvVQYBsbtLG94sSAGUToxjhw9GQhkukkSLvMIYMRKt-R5M2walOq63_eJx17y7HQxlxjOffR41wq4sANewOFlIOuf2czn2QP_FuVg_2SwUpeQctMGYwSgLC7H2gUPFtWzSWB9AhG4Pfwfmy0cd4mxZrrPBglehg8_VpK0pzXpEDMYJ-mBtpZOkAaTXSr0f8aw8Y2CMCmLgRUtC5_JA9NtLhJ6TEBDeNco8Fj6ZfMyZEDxB6xg3MD0fgs7fkbqSUGj4bV54DbvrniSKQ9GwZM29tHXI1xzrvnTg0NIg1ul4kOSWiVeW88-PUlUTXA9kzKsE20CL_CrbOJUlaFhSYHMz-KAisKgtUynMe0lUg");
					// response.setHeader("Authorization",
					// "Bearer pOuJzg0Y0FtJpO-Ugson2P_8dbmvaDiBZs7Z-v_2eIgGgUfFsiWS-TOIkNlquCQGHuu3OyeWCFVIB7xJjEp6nKg9iY1-gzRfjHtdvBk7MVemtm6Ksf5AmmKmuZleLR_-bNMorwUOqTs0BCjAomM6h2peaXA3LTJ_-V6CDpnKVmmfTf0DvVQYBsbtLG94sSAGUToxjhw9GQhkukkSLvMIYMRKt-R5M2walOq63_eJx17y7HQxlxjOffR41wq4sANewOFlIOuf2czn2QP_FuVg_2SwUpeQctMGYwSgLC7H2gUPFtWzSWB9AhG4Pfwfmy0cd4mxZrrPBglehg8_VpK0pzXpEDMYJ-mBtpZOkAaTXSr0f8aw8Y2CMCmLgRUtC5_JA9NtLhJ6TEBDeNco8Fj6ZfMyZEDxB6xg3MD0fgs7fkbqSUGj4bV54DbvrniSKQ9GwZM29tHXI1xzrvnTg0NIg1ul4kOSWiVeW88-PUlUTXA9kzKsE20CL_CrbOJUlaFhSYHMz-KAisKgtUynMe0lUg");
					// httpget.addHeader("Authorization",
					// "Bearer pOuJzg0Y0FtJpO-Ugson2P_8dbmvaDiBZs7Z-v_2eIgGgUfFsiWS-TOIkNlquCQGHuu3OyeWCFVIB7xJjEp6nKg9iY1-gzRfjHtdvBk7MVemtm6Ksf5AmmKmuZleLR_-bNMorwUOqTs0BCjAomM6h2peaXA3LTJ_-V6CDpnKVmmfTf0DvVQYBsbtLG94sSAGUToxjhw9GQhkukkSLvMIYMRKt-R5M2walOq63_eJx17y7HQxlxjOffR41wq4sANewOFlIOuf2czn2QP_FuVg_2SwUpeQctMGYwSgLC7H2gUPFtWzSWB9AhG4Pfwfmy0cd4mxZrrPBglehg8_VpK0pzXpEDMYJ-mBtpZOkAaTXSr0f8aw8Y2CMCmLgRUtC5_JA9NtLhJ6TEBDeNco8Fj6ZfMyZEDxB6xg3MD0fgs7fkbqSUGj4bV54DbvrniSKQ9GwZM29tHXI1xzrvnTg0NIg1ul4kOSWiVeW88-PUlUTXA9kzKsE20CL_CrbOJUlaFhSYHMz-KAisKgtUynMe0lUg");
					Client client = new Client();
					// response = client.
					response = httpclient.execute(httpget);
					// response.addHeader("Authorization",
					// "Bearer pOuJzg0Y0FtJpO-Ugson2P_8dbmvaDiBZs7Z-v_2eIgGgUfFsiWS-TOIkNlquCQGHuu3OyeWCFVIB7xJjEp6nKg9iY1-gzRfjHtdvBk7MVemtm6Ksf5AmmKmuZleLR_-bNMorwUOqTs0BCjAomM6h2peaXA3LTJ_-V6CDpnKVmmfTf0DvVQYBsbtLG94sSAGUToxjhw9GQhkukkSLvMIYMRKt-R5M2walOq63_eJx17y7HQxlxjOffR41wq4sANewOFlIOuf2czn2QP_FuVg_2SwUpeQctMGYwSgLC7H2gUPFtWzSWB9AhG4Pfwfmy0cd4mxZrrPBglehg8_VpK0pzXpEDMYJ-mBtpZOkAaTXSr0f8aw8Y2CMCmLgRUtC5_JA9NtLhJ6TEBDeNco8Fj6ZfMyZEDxB6xg3MD0fgs7fkbqSUGj4bV54DbvrniSKQ9GwZM29tHXI1xzrvnTg0NIg1ul4kOSWiVeW88-PUlUTXA9kzKsE20CL_CrbOJUlaFhSYHMz-KAisKgtUynMe0lUg");
					break;
				}
			} catch (Exception e) {

				Log.e(TAG, e.getLocalizedMessage(), e);

			}

			return response;
		}

		private String inputStreamToString(InputStream is) {

			String line = "";
			StringBuilder total = new StringBuilder();

			// Wrap a BufferedReader around the InputStream
			BufferedReader rd = new BufferedReader(new InputStreamReader(is));

			try {
				// Read response until the end
				while ((line = rd.readLine()) != null) {
					total.append(line);
				}
			} catch (IOException e) {
				Log.e(TAG, e.getLocalizedMessage(), e);
			}

			// Return full string
			return total.toString();
		}

	}
}
